import { Navigate, useLocation } from "react-router";

const PrivateRoute = ({ children }) => {
  const location = useLocation();

  // 从localStorage获取token，判断用户是否已登录
  const isAuthenticated = localStorage.getItem("token");

  // 如果用户未登录，重定向到登录页面，并记录当前URL
  if (!isAuthenticated) {
    return <Navigate to="/login" state={{ from: location }} replace />;
  }

  // 如果用户已登录，显示受保护的路由内容
  return children;
};

export default PrivateRoute;
